/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webservice;

import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import metier.Document;
import metier.GestionDocuments;

/**
 *
 * @author laurent
 */
@WebService(serviceName = "GestionDocumentsWS")
public class GestionDocumentsWS {
    
    @PostConstruct
    public void initialisation() {
        GestionDocuments.getInstance();
    }
    
    @PreDestroy
    public void destruction() {
        GestionDocuments.getInstance().destruction();
    }

    /** This is a sample web service operation */
    @WebMethod(operationName = "hello")
    public String hello(@WebParam(name = "name") String txt) {
        return "Hello " + txt + " !";
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "depotDocument")
    public String depotDocument(@WebParam(name = "fileName") String fileName, @WebParam(name = "fileContent") byte[] fileContent) {
        String result;
        try {
        GestionDocuments.getInstance().depotDocument(fileContent, fileName);
        result = "Fichier déposé sur le serveur";
        } catch(Exception e) {
            result = "Erreur lors de l'écriture du fichier";
        }
        return result;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "rechercheDocuments")
    public String rechercheDocuments(@WebParam(name = "motsCles") List<String> motsCles) {
        String result;
        try {
            result = GestionDocuments.getInstance().rechercheDocument(motsCles);
        } catch (Exception e) {
            result = "Erreur lors de la recherche";
        }
        return result;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "retourneDocument")
    public Document retourneDocument(@WebParam(name = "idDocument") Integer idDocument) {
        Document result;
        try {
            result = GestionDocuments.getInstance().retourneDocument(idDocument);
        } catch(Exception e) {
            result = new Document("Fichier introuvable", new byte[1]);
        }
        return result;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "genererPDF")
    public Document genererPDF(@WebParam(name = "idDocument") Integer idDocument, @WebParam(name = "fopFile") byte[] fopFile) {
        Document result;
        try {
            result = GestionDocuments.getInstance().genererPDF(fopFile, idDocument);
        } catch(Exception e) {
            result = new Document("Erreur", new byte[1]);
        }
        return result;
    }
}